package modelCollection;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import dataCollection.AbstractDetail;
import dataCollection.AbstractDetailTreeAlgorithms;
import dataCollection.Aptitude;
import dataCollection.InvalidDetailException;
import dataCollection.Trait;
import dataCollection.Tree;
import dataCollection.TreeElement;

public class XMLAptitudesReader {
  private XMLAptitudesReader() {}
  
  public static void readApts(Tree<AbstractDetail> aptitudes) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
      builder = factory.newDocumentBuilder();
      Document document = builder.parse( new File("bin/resources/aptitudes_en.xml"));
      readDoc(document, aptitudes);
    } catch (ParserConfigurationException | SAXException | IOException e) {
      e.printStackTrace();
    }
  }
  
  private static void readDoc (Document doc, Tree<AbstractDetail> ability_tree) {
    NodeList entries = doc.getElementsByTagName("aptitude");
    for(int i = 0; i < entries.getLength(); ++i) {
      System.out.println("-");
      String apt_name = null;
      String trait_name = null;
      String apt_desc = null;
      int init_val = 0;
      Element node = (Element) entries.item(i);
      NamedNodeMap attributes = node.getAttributes(); 
      for(int j = 0; j < attributes.getLength(); ++j) {
        Attr attr = (Attr)attributes.item(j);
        if(attr.getNodeName().equals("id")) {
          apt_name = new String(attr.getNodeValue());
        }
      }
      
      NodeList children = node.getChildNodes();
      for(int k = 0; k < children.getLength(); ++k) {
        if(children.item(k).getNodeName().equals("description")) {
          apt_desc = new String(children.item(k).getTextContent());
        } else if(children.item(k).getNodeName().equals("trait")) {
          trait_name = new String(children.item(k).getTextContent());
        } else if(children.item(k).getNodeName().equals("startval")) {
          init_val = Integer.parseInt(children.item(k).getTextContent());
        }
      }
      System.out.println("NODE: " + apt_name + " -- " + trait_name + " -- " + init_val);
      TreeElement<AbstractDetail> trait_node = null;
      try {
        trait_node = AbstractDetailTreeAlgorithms.getTraitNode(ability_tree, trait_name);
        Aptitude new_apt = new Aptitude(apt_name, apt_desc, (Trait)trait_node.getItem(), init_val);
        ability_tree.setTop(trait_node);
        ability_tree.pushAndStay(new_apt);
      } catch (InvalidDetailException e) {
        e.printStackTrace();
      }
    }
  }
}
